<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 对角线遍历 -->
    <script>
      var findDiagonalOrder = mat => {
        let res = []
        let [m, n] = [mat.length, mat[0].length]
        // 外层遍历次数
        let k = m + n - 1
        // 是否翻转的标志
        let flag = false
        for (let i = 0; i < k; i++) {
          let temp = []
          for (let j = 0; j <= i; j++) {
            // 遇到虚构的，跳过
            if (!mat[i - j] || mat[i - j][j] === undefined) continue
            if (flag) {
              // 倒序
              temp.unshift(mat[i - j][j])
            } else {
              // 正序
              temp.push(mat[i - j][j])
            }
          }
          res.push(...temp)
          // 每遍历一次，下一次翻转
          flag = !flag
        }
        return res
      }
      console.log(
        findDiagonalOrder([
          [1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]
        ])
      )
    </script>
  </body>
</html>
